<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 项目团队数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\ProjectTeamService;
use think\Request;

class ProjectTeamController extends VerifyController
{

    /** @var ProjectTeamService */
    private $projectTeamService;

    public function __construct()
    {
        parent::__construct();
        $this->projectTeamService = ProjectTeamService::getInstance();
    }

    /**
     * 后台配置—保存团队配制
     */
    public function saveConfig(Request $request)
    {
        $param = $request->param()['data'];

        $id = $param['id'] ?? 0;

        $data = [];
        $data['id'] = intval($id);
        $data['name'] = $param['name'];
        $data['type'] = $param['type'];
        $data['role_id'] = $param['role_id'];
        $data['extra_role_id'] = $param['extra_role_id'] ?? '';
        $data['step_category_id'] = $param['step_category_id'] ?? '';
        //新增字段
        $data['step_category_name'] = $param['step_category_name'] ?? '';
        $data['attribute'] = $param['attribute'] ?? '';
        $data['step_ids'] = $param['step_ids'] ?? '';

        $data['sort'] = $param['sort'] ?? 99;
        //单位
        $unit = $param['unit'] ?? 1;
        $data['unit'] = intval($unit);
        $data['status'] = 1;
        $data['is_show'] = 1;

        try {
            $res = $this->projectTeamService->saveTeamConfig($data);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 后台配置—列表
     */
    public function getTeamConfigList(Request $request)
    {
        $param = $request->param()['param'];
        $projectId = $param['project_id'] ?? 0;
        $templateId = $param['project_template_id'] ?? 0;

        try {
            $res = $this->projectTeamService->getTeamConfigList($projectId, $templateId);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 后台配置—删除团队
     */
    public function delConfig(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->projectTeamService->delTeamConfig($param['filter']['ids']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 后台配置—模板团队--显示隐藏
     */
    public function templateTeamConfig(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->projectTeamService->saveTemplateTeamConfig($param['id'], $param['template_id'], $param['type']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 后台配置—团队--增加成员
     */
    public function teamAddUser(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->projectTeamService->teamAddUser($param['project_id'], $param['team_id'], $param['user_id']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 项目团队成员列表
     */
    public function teamUserList(Request $request)
    {
        $param = $request->param()['param'];
        $page = $param['page'] ?? [1, C('default_page_total')];
        try {
            $teamId = $param['team_id'] ?? 0;
            $res = $this->projectTeamService->getTeamUserList($param['project_id'], $teamId, $page);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 项目团队成员列表
     */
    public function teamDelUser(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->projectTeamService->teamDelUser($param['project_id'], $param['team_id'], $param['user_id']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 项目团队增加增加成员角色、删除角色
     */
    public function teamSaveUserRole(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->projectTeamService->teamSaveUserRole($param['project_id'], $param['team_id'], $param['user_id'], $param['role_id'], $param['type']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 根据项目ID--查询成员列表
     */
    public function projectUserList(Request $request)
    {
        $param = $request->param()['param'];
        $stepCategoryId = $param['step_category_id'] ?? 0;
        $isDelete = $param['is_delete'] ?? 0;
        //小环节多个--只查一个
        if ($stepCategoryId) {
            $stepCategoryIdList = explode(',', $stepCategoryId);
            $stepCategoryId = $stepCategoryIdList[0] ?? 0;
        }
        $projectId = $param['project_id'] ?? 0;
        try {
            $res = $this->projectTeamService->getProjectUserList($projectId, $stepCategoryId, $isDelete);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取用户所在团队信息列表
     */
    public function getUserTeamList(Request $request)
    {
        $param = $request->param()['param'];
        try {
            $res = $this->projectTeamService->getUserTeamList($param['project_id'], $param['user_id']);
            return json(success_response('', $res));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }
}
